
using System;
using UnityEngine;
using UnityEngine.UI;

public class NumItem : MonoBehaviour
{
    public bool notIsMergedNumber = true;// 防止重复合并
    
    private Text numText; //上面的随机数值
    public int number
    {
        get =>int.Parse(numText.text);
        set => numText.text = value.ToString();
    }
    void Awake()
    {
        numText = transform.Find("value_text").GetComponent<Text>();
        number = 2;
    }
    
    #region 生成
    private static readonly  GameObject numItemPrefab;//对应的预制体
    
    //生成一个数值Item
    public static NumItem SpawnNumber(Transform parent,int val)
    {
        var go = (GameObject)Instantiate(Resources.Load("Prefab/Game/NumItem"), parent);
        var it = go.GetComponent<NumItem>();
        it.number = val;
        return it;
    }
    #endregion
    #region 游戏逻辑
    
    //更新位置 播放移动动画
    public void UpdatePosition()
    {
        //TODO: 移动动画
        // var oldpos = transform.localPosition;
        transform.localPosition = Vector3.zero;
    }
    
    //被别人合并
    public void Merged()
    {
        //TODO : 更新位置 随后销毁自己
        // var oldpos = transform.localPosition;
        transform.localPosition = Vector3.zero;
        GameObject.Destroy(gameObject); //销毁自己
    }
    
    //合并别人
    public void Merge()
    {
        // 合并数值 x2 
        number *= 2;
        //TODO:播放合并动画
    }
    
    #endregion

}
